package scc.project.web.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import scc.project.entity.Custom;
import scc.project.entity.vo.JsonVo;
import scc.project.entity.vo.PageVo;
import scc.project.entity.vo.RestFulPageVo;
import scc.project.service.CustomService;

/**
 * 客户管理的控制器
 *
 * @author 陈圳佳
 *         日期：2015/6/22
 */
@Controller
@RequestMapping("/manage/custom")
public class CustomerController {

    @Autowired
    private CustomService customService;

    /**
     * 添加编辑客户信息
     */
    @RequestMapping(value = "/edit.html", method = RequestMethod.GET)
    public String edit(ModelMap modelMap) {
        modelMap.addAttribute("action", "/manage/custom");
        return "/manage/custom/edit";
    }

    /**
     * 修改编辑客户信息
     */
    @RequestMapping(value = "/{id}.html", method = RequestMethod.GET)
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        Custom custom = customService.get(id);
        modelMap.addAttribute("custom", custom);
        modelMap.addAttribute("action", "/manage/custom");
        return "/manage/custom/edit";
    }

    /**
     * 搜索客户支持翻页
     *
     * @param page      页码
     * @param customNme 客户名称
     * @param address   客户地址
     */
    @RequestMapping(value = "/list/{page}.html", method = RequestMethod.GET)
    public String list(@PathVariable("page") Integer page,
                       @RequestParam(value = "name", required = false) String customNme,
                       @RequestParam(value = "address", required = false) String address,
                       ModelMap modelMap) {

        PageVo<Custom> pageVo;
        try {
            pageVo = RestFulPageVo.convert(customService.find(page, address, customNme), ".html");
            pageVo.addProperty("data-pjax", "#content");
            modelMap.addAttribute("pageVo", pageVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/manage/custom/list";
    }

    /**
     * 添加客户，只支持ajax 的POST 请求
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JsonVo add(@RequestBody Custom custom) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = customService.save(custom);
            jsonVo.setStatus(b).setMsg(b ? "添加成功" : "添加失败原因未知？");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setMsg(e.getMessage()).setStatus(false);
        }
        return jsonVo;
    }

    /**
     * 删除客户，只支持ajax 的POST 请求
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonVo delete(@PathVariable("id") Long id) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = customService.delete(id);
            jsonVo.setStatus(b).setMsg(b ? "删除成功" : "删除失败原因未知");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setStatus(false).setMsg(e.getMessage());
        }
        return jsonVo;
    }

    /**
     * 修改的请求，返回JSON，接收的也是JSON
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public JsonVo update(@RequestBody Custom custom) {
        JsonVo jsonVo = new JsonVo();
        try {
            boolean b = customService.updateById(custom);
            jsonVo.setStatus(b).setMsg(b ? "修改成功" : "修改失败原因未知？");
        } catch (Exception e) {
            e.printStackTrace();
            jsonVo.setMsg(e.getMessage()).setStatus(false);
        }
        return jsonVo;
    }


    /**
     * 搜索客户支持翻页，但是返回的是 JSON
     *
     * @param page      页码
     * @param customNme 客户名称
     * @param address   客户地址
     */
    @ResponseBody
    @RequestMapping(value = "/list/{page}", method = RequestMethod.GET)
    public JsonVo list(@PathVariable("page") Integer page,
                       @RequestParam(value = "customName", required = false) String customNme,
                       @RequestParam(value = "address", required = false) String address) {
        JsonVo jsonVo = new JsonVo();
        try {
            PageVo<Custom> pageVo = customService.find(page, customNme, address);
            jsonVo.addValue("total", pageVo.getData()).addValue("totalPage", pageVo.getTotalPage()).addValue("data", pageVo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonVo;
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Custom get(@PathVariable("id") Long id) {
        return customService.get(id);
    }
}
